package com.own.business.system.menu.business.service.impl;

import com.own.business.system.menu.business.service.SystemRolePermissionService;
import com.own.business.system.menu.business.service.SystemRoleService;
import com.own.business.system.menu.business.service.SystemUserRoleService;
import com.own.business.system.menu.entity.bo.SystemRolePermissionBo;
import com.own.business.system.menu.entity.map.SystemRolePermissionMapVo;
import com.own.business.system.menu.entity.po.SystemRolePermission;
import com.own.business.system.menu.entity.query.SystemRolePermissionQuery;
import com.own.business.system.menu.entity.vo.SystemRolePermissionVo;
import com.own.business.system.menu.mapper.SystemRolePermissionMapper;
import com.own.component.common.base.entity.BaseBo;
import com.own.component.common.base.entity.po.BaseBean;
import com.own.component.common.base.service.impl.AbstractBaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * SystemRolePermissionServiceImpl
 *
 * @author chenxueli
 * @date 2024-03-31 09:22:58
 */
@Slf4j
@Service
public class SystemRolePermissionServiceImpl extends AbstractBaseService<
        SystemRolePermission,
        SystemRolePermissionBo,
        SystemRolePermissionVo,
        SystemRolePermissionMapVo,
        SystemRolePermissionQuery,
        SystemRolePermissionMapper
        > implements SystemRolePermissionService {

    @Resource
    private SystemRolePermissionMapper systemRolePermissionMapper;

    @Resource
    private SystemUserRoleService systemUserRoleService;
    @Resource
    private SystemRoleService systemRoleService;

    /**
     * 根据用户id查询角色权限列表
     *
     * @param userId 用户id
     * @return 角色权限列表
     */
    @Override
    public List<SystemRolePermission> listByUserId(Long userId) {
        // 获取用户的角色id列表
        var roleIdList = systemUserRoleService.listRoleIdListByUserId(userId);
        // 根据角色id列表获取角色权限列表
        return listByRoleIdList(roleIdList);
    }

    /**
     * 根据用户id查询角色权限列表
     *
     * @param userId   用户id
     * @param clientId 客户端id
     * @return 角色权限列表
     */
    @Override
    public List<SystemRolePermission> listByUserId(Long userId, String clientId) {
        var roleList = systemRoleService.listByUserId(userId, clientId);
        var roleIdList = roleList.stream().map(BaseBean::getId).toList();
        // 根据角色id列表获取角色权限列表
        return listByRoleIdList(roleIdList);
    }

    /**
     * 根据角色id查询角色权限列表
     *
     * @param roleId 角色id
     * @return 角色权限列表
     */
    @Override
    public List<SystemRolePermission> listByRoleId(Long roleId) {
        var wrapper = lambdaQueryWrapper().eq(SystemRolePermission::getFkRoleId, roleId);
        return listByWrapper(wrapper);
    }

    /**
     * 根据角色id列表查询角色权限列表
     *
     * @param roleIdList 角色id列表
     * @return 角色权限列表
     */
    @Override
    public List<SystemRolePermission> listByRoleIdList(List<Long> roleIdList) {
        if (roleIdList == null || roleIdList.isEmpty()) {
            return new ArrayList<>();
        }
        var wrapper = lambdaQueryWrapper().in(SystemRolePermission::getFkRoleId, roleIdList);
        return listByWrapper(wrapper);
    }

    /**
     * 保存权限数据
     *
     * @param roleId      角色id
     * @param permissions 权限列表
     */
    @Override
    public void save(Long roleId, List<SystemRolePermissionBo> permissions) {
        // 删除已有的信息
        var deleteWrapper = lambdaQueryWrapper().eq(SystemRolePermission::getFkRoleId, roleId);
        systemRolePermissionMapper.delete(deleteWrapper);
        // 表单信息转持久化对象
        var list = permissions.stream().map(BaseBo::toPo).peek(item -> {
            item.setFkRoleId(roleId);
        }).toList();
        mapperUtil.batchInsert(systemRolePermissionMapper, list);
    }

    /**
     * 根据角色id删除
     *
     * @param roleId 角色id
     */
    @Override
    public void deleteByRoleId(Long roleId) {
        var wrapper = lambdaQueryWrapper().eq(SystemRolePermission::getFkRoleId, roleId);
        systemRolePermissionMapper.delete(wrapper);
    }

    /**
     * 根据角色id列表和菜单id查询权限标识列表
     *
     * @param roleIdList 角色id列表
     * @param menuId     菜单id
     * @return 权限标识列表
     */
    @Override
    public List<String> listPermissionByRoleIdListAndMenuId(Collection<Long> roleIdList, Long menuId) {
        if (roleIdList == null || roleIdList.isEmpty()) {
            return new ArrayList<>();
        }
        var wrapper = lambdaQueryWrapper()
                .select(SystemRolePermission::getPermission)
                .in(SystemRolePermission::getFkRoleId, roleIdList)
                .eq(SystemRolePermission::getFkMenuId, menuId);
        return listByWrapper(wrapper).stream().map(SystemRolePermission::getPermission).toList();
    }

}
